package com.rmondjone.view;

import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.rmondjone.androidcamerademo.R;


/**
 * copyright：""
 * author：zxy
 * Version：1.0
 * creation date：2018/2/2
 * describe： 基础弹框
 */
public abstract class BaseDialog extends DialogFragment {
    private static final String MARGIN = "margin";
    private static final String WIDTH = "width";
    private static final String HEIGHT = "height";
    private static final String DIM = "dim_amount";
    private static final String BOTTOM = "show_bottom";
    private static final String CANCEL = "out_cancel";
    private static final String ANIM = "anim_style";
    private static final String LAYOUT = "layout_id";

    private int margin;//左右边距
    private int width;//宽度
    private int height;//高度
    private float dimAmount = 0.7f;//灰度深浅
    private boolean showBottom;//是否底部显示
    private boolean outCancel = true;//是否点击外部取消
    @StyleRes
    private int animStyle;
    @LayoutRes
    protected int layoutId;

    public Context mContext;
    public AppCompatActivity mActivity;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.NiceDialog);
        layoutId = getMainContentViewId();
        mContext = getContext();
        mActivity = (AppCompatActivity) getActivity();
        //恢复保存的数据
        if (savedInstanceState != null) {
            margin = savedInstanceState.getInt(MARGIN);
            width = savedInstanceState.getInt(WIDTH);
            height = savedInstanceState.getInt(HEIGHT);
            dimAmount = savedInstanceState.getFloat(DIM);
            showBottom = savedInstanceState.getBoolean(BOTTOM);
            outCancel = savedInstanceState.getBoolean(CANCEL);
            animStyle = savedInstanceState.getInt(ANIM);
            layoutId = savedInstanceState.getInt(LAYOUT);
        }
    }

    protected abstract int getMainContentViewId();

//    @NonNull
//    @Override
//    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
//        return new AvoidLeakDialog(requireContext(), getTheme());
//    }

    protected View mView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        LogUtil.e("msg      onCreateView ===  " + getClass().getSimpleName());

        if (mView == null) {
//            dataBinding = DataBindingUtil.inflate(inflater, layoutId, container, false);
            mView = inflater.inflate(layoutId, container, false);
        } else {
            ViewGroup viewGroup = (ViewGroup) mView.getParent();
            if (viewGroup != null) {
                viewGroup.removeView(mView);
            }
        }
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        LogUtil.e("msg      onViewCreated ===  " + getClass().getSimpleName() + " code== " + getClass().hashCode());
        initComponents();
        initRecyclerView();
        initData();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        LogUtil.e("msg      onDestroy ===  " + getClass().getSimpleName() + " code== " + getClass().hashCode());

    }

    protected abstract void initComponents();

    protected abstract void initData();


    public void initRecyclerView() {

    }

    @Override
    public void onStart() {
        super.onStart();
        initParams();
    }

    public void showCenterToast(String msg, boolean success) {
//        ToastUtil.showCenterToast(mContext, msg, success);
    }

    /**
     * 屏幕旋转等导致DialogFragment销毁后重建时保存数据
     *
     * @param outState
     */


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(MARGIN, margin);
        outState.putInt(WIDTH, width);
        outState.putInt(HEIGHT, height);
        outState.putFloat(DIM, dimAmount);
        outState.putBoolean(BOTTOM, showBottom);
        outState.putBoolean(CANCEL, outCancel);
        outState.putInt(ANIM, animStyle);
        outState.putInt(LAYOUT, layoutId);
    }

    private void initParams() {
        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            //调节灰色背景透明度[0-1]，默认0.5f
            lp.dimAmount = dimAmount;
            //是否在底部显示
            if (showBottom) {
                lp.gravity = Gravity.BOTTOM;
                if (animStyle == 0) {
                    animStyle = R.style.DefaultAnimation;
                }
            }

            //设置dialog宽度
            if (width == 0) {
                lp.width = getScreenWidth(getContext()) - 2 * dp2px(getContext(), margin);
            } else if (width == -1) {
                lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
            } else {
                lp.width = dp2px(getContext(), width);
            }
            //设置dialog高度
            if (height == -1) {
                lp.height = WindowManager.LayoutParams.MATCH_PARENT;
            } else if (height > 0) {
                if (heightIsDp) {
                    lp.height = dp2px(getContext(), height);
                } else {
                    lp.height = height;
                }
            } else {
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

            }

            //设置dialog进入、退出的动画
            window.setWindowAnimations(animStyle);
            window.setAttributes(lp);
        }
        setCancelable(outCancel);
    }

    public BaseDialog setMargin(int margin) {
        this.margin = margin;
        return this;
    }

    public BaseDialog setWidth(int width) {
        this.width = width;
        return this;
    }

    public BaseDialog setHeight(int height) {
//        this.height = height;
        setHeight(height, true);
        return this;
    }

    //是否为 dp 高度换算
    boolean heightIsDp = false;

    public BaseDialog setHeight(int height, boolean isDp) {
        this.height = height;
        heightIsDp = isDp;
        return this;
    }

    public BaseDialog setDimAmount(float dimAmount) {
        this.dimAmount = dimAmount;
        return this;
    }

    public BaseDialog setShowBottom(boolean showBottom) {
        this.showBottom = showBottom;
        return this;
    }

    public BaseDialog setOutCancel(boolean outCancel) {
        this.outCancel = outCancel;
        return this;
    }

    public BaseDialog setAnimStyle(@StyleRes int animStyle) {
        this.animStyle = animStyle;
        return this;
    }


    public int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public int getScreenWidth(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return displayMetrics.widthPixels;
    }


    public boolean isShowing() {
        if (getDialog() != null && getDialog().isShowing()) {
            return true;
        }

        return false;
    }

    public BaseDialog show(FragmentManager manager) {
        //已经添加

        if (getDialog() != null && getDialog().isShowing()) {
            return this;
        }

        FragmentTransaction fragmentTransaction = manager.beginTransaction();

        if (isAdded()) {
            fragmentTransaction.remove(this);
        }

        try {
            if (!isAdded())
                fragmentTransaction.add(this, getClass().getSimpleName()).commitAllowingStateLoss();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return this;
    }

    /**
     * 隐藏
     */
    @Override
    public void dismiss() {
        if (getDialog() != null && getDialog().isShowing()) {

            try {
                super.dismiss();

            } catch (Exception e) {


            }
        }
    }

    //弹窗
    public void showdebugToast(String msg) {
//        ToastUtil.showDebugToast(getContext(), msg);
    }

    //    @Override
    public void showDebugToast(String msg) {

    }


}
